package games;

import javax.swing.*;
import java.util.Scanner;

public class Property extends Square{
    Player owner;
    double price;
    double ipotecPrice;
    double rent1;//only for this
    double rent2;//for group owner
    boolean groupComplited;
    int groupId;

    public Property(String n, int g, double p, double r, double r2){
        super(n);
        groupId = g;
        price = p;
        ipotecPrice = p/2;
        rent1 = r;
        rent2 = r2;
        owner = null;
        groupComplited = false;
    }

    double calculateRent(){
        //TODO Group Rent Calculation Missing  Şamil
        double curRent = 0;
        if (groupComplited){
            curRent = rent2;
        }
        else {
            curRent = rent1;
        }
        return curRent;
        
    }

    void onMyProperty(){}

    public void onSquare(Player p){
    	Scanner in = new Scanner(System.in);
    	int choice;
        if(owner == null)
        {
        	System.out.println("Do you wanna buy "+name+" ?  1/2");
        	
            //choice = in.nextInt();
            choice = JOptionPane.showConfirmDialog(
                    new JFrame(),
                    "Do you wanna buy "+name+" ?",
                    "No Owner",
                    JOptionPane.YES_NO_OPTION);
            
            switch (choice){
                case 0:
                    if(p.loseMoney(price)){
                        this.owner = p;
                        p.addProperty(this);
                        p.isGroupComplited(this);
                        System.out.println(p.getPlayerName()+": I bought "+this.name);
                    }
                    else{
                        System.out.println(p.getPlayerName()+": You dont have enough money for "+this.name);
                    }
                    break;
                case 1:
                    System.out.println(p.getPlayerName()+": I dont wanna buy "+this.name);
                    break;
                default:
                    System.out.println("Do nothing please choose one of offered options.");
                    break;
            }
        }
        else if (owner == p){
            System.out.println("home sweet home :)");
            onMyProperty();
        }
        else{
            double transfer = calculateRent();
            if(p.loseMoney(transfer)){
                owner.earnMoney(transfer);
            }
            else{ // iflas etti herşeyi al
                this.owner.earnMoney(p.getMoney());
                for(int i=0;i<p.myProperties.size();i++){
                    p.myProperties.elementAt(i).owner=this.owner;
                    this.owner.addProperty(p.myProperties.elementAt(i));
            	}
            }
        }
    }
}
